//
//  WebViewController.m
//  iosweb
//
//  Created by 廉英雷 on 15/11/16.
//  Copyright © 2015年 廉英雷. All rights reserved.
//

#import "WebViewController.h"

@interface WebViewController ()<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
      NSString *path = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"html"];
      NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:path]];
      [self.webView loadRequest:request];
      
      self.webView.delegate = self;
      
      UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
      btn.backgroundColor = [UIColor whiteColor];
      [btn setTitle:@"返回" forState:UIControlStateNormal];
      [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
      [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
      UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithCustomView:btn];
      self.navigationItem.leftBarButtonItem = bar;
      //self.navigationItem.rightBarButtonItem = bar;
      
      //检测网页上的是否有电话，有的话点击可以拨打
     //self.webView.detectsPhoneNumbers = YES;
      //网页缩放适应屏幕
      //self.webView.scalesPageToFit = YES;
      //27.这个属性决定了CSS属性是采用column-break 还是page-breaking样式。
//      UIWebPaginationBreakingModePage,
//      UIWebPaginationBreakingModeColumn
      self.webView.paginationBreakingMode =UIWebPaginationBreakingModeColumn;
}

- (void)btnClick{
      
      [self.webView goBack];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
      if (navigationType == UIWebViewNavigationTypeLinkClicked) {
            NSLog(@"用户触发了一个链接事件");
      }
      
//      typedef NS_ENUM(NSInteger, UIWebViewNavigationType) {
//            UIWebViewNavigationTypeLinkClicked,//用户触发了一个链接
//            UIWebViewNavigationTypeFormSubmitted,//用户提交了一个表单
//            UIWebViewNavigationTypeBackForward,//用户触击前进前进或返回按钮
//            UIWebViewNavigationTypeReload,//用户触击重新加载的按钮
//            UIWebViewNavigationTypeFormResubmitted,//用户重复提交表单
//            UIWebViewNavigationTypeOther//发生了其他行为
//      };
      BOOL isOpen = YES;
      NSString *str = request.URL.absoluteString;
      NSArray *ary = request.URL.pathComponents;
      
      if (ary.count>2) {
            NSString *header = ary[1];
            if ([request.URL.scheme hasPrefix:@"yuexianghulian"]) {
                  SEL func = NSSelectorFromString(header);
                  
                  [self performSelector:func withObject:ary[2]];
                  
            }

      }
      
      NSLog(@"-----%@",request.URL.scheme);
     
      NSLog(@"请求的---%@", request.URL.absoluteString);
      if ([request.URL.scheme isEqualToString:@"http"]) {
            NSLog(@"==============");
            
            [webView stringByEvaluatingJavaScriptFromString:@"clickme()"];
            isOpen = NO;
      }
      if ([request.URL.absoluteString isEqualToString:@"http://m.baidu.com/"]) {
            
            [webView stringByEvaluatingJavaScriptFromString:@"btnClick()"];
            isOpen = YES;
      }
      
      return isOpen;
}

- (void)showMessage321:(NSString *)mgr {
      
      [self showTipView:mgr];

}
- (void)webViewDidFinishLoad:(UIWebView *)webViewE{
      
      [webViewE stringByEvaluatingJavaScriptFromString:@"clickme()"];
     
      
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
